package com.drinks.yiniang.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.listview.RefundReasonAdapter;
import com.drinks.yiniang.entity.goods.RefundGoodsJson;
import com.hansen.library.Constants;
import com.hansen.library.listener.CheckDoubleClickListener;
import com.hansen.library.ui.widget.dialog.BaseDialogFragment;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 退款原因dialog
 * @author han
 * @date 2019/11/12
 */
public class RefundReasonDialog extends BaseDialogFragment implements AdapterView.OnItemClickListener {

    //退款原因列表
    private ListView lv_refund_reason_dialog;
    //关闭
    private Button btn_refund_reason_close;

    private RefundReasonAdapter mRefundReasonAdapter;

    private List<RefundGoodsJson.RefundGoodsReason> mList;

    private OnRefundReasonChooseListener onRefundReasonChooseListener;

    private int mCurrentChoosePostion = -1;

    public static RefundReasonDialog newInstance(List<RefundGoodsJson.RefundGoodsReason> mList) {

        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.KeyObject, new ArrayList<>(mList));

        RefundReasonDialog dialog = new RefundReasonDialog();
        dialog.setArguments(bundle);

        return dialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            onRefundReasonChooseListener = (OnRefundReasonChooseListener) context;
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + "must implement OnRefundReasonChooseListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 定义Dialog布局和参数
        mDialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);

        // 获取Dialog布局
        View mView = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_refund_reason, null);

        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        mDialog.setContentView(mView);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);

        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.dimAmount = 0.5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平

        dialogWindow.setAttributes(lp);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        lv_refund_reason_dialog = mView.findViewById(R.id.lv_refund_reason_dialog);

        btn_refund_reason_close = mView.findViewById(R.id.btn_refund_reason_close);

        lv_refund_reason_dialog.setOnItemClickListener(this);

        btn_refund_reason_close.setOnClickListener(new CheckDoubleClickListener() {
            @Override
            public void onCheckDoubleClick(View v) {
                dismiss();
            }
        });

        initData();

        return mDialog;
    }

    private void initData() {
        mContext = getActivity();

        mList = getArguments() != null ? (List<RefundGoodsJson.RefundGoodsReason>) getArguments().getSerializable(Constants.KeyObject) : new ArrayList<RefundGoodsJson.RefundGoodsReason>();

        mRefundReasonAdapter = new RefundReasonAdapter(mContext, mList);

        lv_refund_reason_dialog.setAdapter(mRefundReasonAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(CommonUtils.isArrayIndexOutOfBounds(mList, position)) {
            return;
        }

        if(!CommonUtils.isArrayIndexOutOfBounds(mList, mCurrentChoosePostion)) {
            mList.get(mCurrentChoosePostion).setChecked(false);
        }

        RefundGoodsJson.RefundGoodsReason data = mList.get(position);
        data.setChecked(true);
        mList.set(position, data);

        mCurrentChoosePostion = position;

        mRefundReasonAdapter.notifyDataSetChanged();

        if(onRefundReasonChooseListener != null) {
            onRefundReasonChooseListener.onReasonChooseClick(data.getId(), data.getName());
        }

        dismiss();
    }

    @Override
    public void onDestroy() {
        mDialog = null;
        super.onDestroy();
    }

    public interface OnRefundReasonChooseListener {
        void onReasonChooseClick(String code, String value);
    }
}
